summaryrefslogtreecommitdiff
path: root/src/pages/[locale]/rss.xml.ts
blob: 9d2e34b45fd2cff8437f9f27d9e90e46edeee194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import rss, { RSSFeedItem } from '@astrojs/rss';
import { CollectionEntry, getCollection } from 'astro:content';
import { getDate, getHref, isNewsItemInLocale } from '$lib/newsUtils';
import localeStaticPaths from '$lib/localeStaticPaths';
import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import type { APIContext } from 'astro';
import translate from '$i18n/translate';
import tFeed from '$i18n/translations/pages/rss.xml';
import type LallansIssue from '$types/LallansIssue';
import type Locale from '$types/Locale';
import { getAllLallansIssues } from '$data/lallans';
import type Scotsoun from '$types/Scotsoun';
import { getAllScotsoun } from '$data/scotsoun';

export async function get(context: APIContext) {
  const locale = getLocaleFromPathOrThrow(context.url.pathname);
  const t = translate(locale);
  const site = context.site;
  if (!site) {
    throw new Error('No site provided in Astro configuration');
  }

  const news = (await getCollection('news')).filter((item) => isNewsItemInLocale(item, locale));
  const lallans = await getAllLallansIssues();
  const scotsoun = await getAllScotsoun();

  return rss({
    title: t(tFeed, { key: 'title' }),
    xmlns: { atom: 'http://www.w3.org/2005/Atom' },
    description: t(tFeed, { key: 'description' }),
    site,
    items: [
      ...news.map(newsItemToRssFeedItem),
      ...lallans.map(lallansIssueToRssFeedItem(locale)),
      ...scotsoun.map(scotsounReleaseToRssFeedItem(locale)),
    ],
    customData: `
      <language>${locale}</language>
      <atom:link href="${site}${locale}/rss.xml" rel="self" type="application/rss+xml" />
    `,
  });
}

function newsItemToRssFeedItem(item: CollectionEntry<'news'>): RSSFeedItem {
  const pubDate = getDate(item);
  if (!pubDate) {
    throw new Error(`Could not determine publication date of ${item}`);
  }

  return {
    link: getHref(item),
    title: item.data.title,
    description: item.data.summary,
    pubDate,
  };
}

function lallansIssueToRssFeedItem(locale: Locale) {
  return function (issue: LallansIssue): RSSFeedItem {
    return {
      link: `/${locale}/furthsettins/lallans/${issue.issueNumber}`,
      title: `Lallans ${issue.issueNumber}: ${issue.issueName}`,
      description: issue.description[locale],
      pubDate: new Date(issue.uploadDate),
    };
  };
}

function scotsounReleaseToRssFeedItem(locale: Locale) {
  return function (release: Scotsoun): RSSFeedItem {
    return {
      link: `/${locale}/furthsettins/scotsoun/${release.scotsounId}`,
      title: `Scotsoun SSCD${release.scotsounId}: ${release.title}`,
      description: release.description?.[locale] ?? release.longName,
      pubDate: new Date(release.uploadDate),
    };
  };
}

export async function getStaticPaths() {
  return localeStaticPaths;
}